extends Node3D

const PickUp = preload("res://gameObjects/interactables/pick_upable.tscn")

@export var paused := false

@onready var inventory_interface = $CanvasLayer/InventoryInterface
@onready var player = $player
@onready var hot_bar = $CanvasLayer/HotBar

func _ready():
	player.toggle_inventory.connect(toggle_inventory_interface)
	inventory_interface.set_player_inventory_data(player.inventory)
	hot_bar.set_inventory_data(player.inventory)

	for node in get_tree().get_nodes_in_group("external_inventory"):
		node.toggle_inventory.connect(toggle_inventory_interface)

func toggle_inventory_interface(external_inventory_owner = null):
	inventory_interface.visible = not inventory_interface.visible
	if not inventory_interface.visible:
		inventory_interface.interface_closed()

	if inventory_interface.visible:
		self.paused = true
		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
		hot_bar.hide()
	else:
		self.paused = false
		Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
		hot_bar.show()

	if external_inventory_owner and inventory_interface.visible:
		inventory_interface.set_external_inventory(external_inventory_owner)
	else:
		inventory_interface.clear_external_inventory()

func _on_inventory_interface_drop_slot_data(slot_data):
	var pickup = PickUp.instantiate()
	pickup.player = player
	pickup.slot_data = slot_data
	pickup.position = player.get_drop_position()
	add_child(pickup)
